BITS 32

section .text
global _start
global runtimeReadTSC
global runtimeHalt
global runtimePause
global memset
global memcpy
global memmove

extern kernel_entry

_start:
    call kernel_entry

.halt:
    hlt
    jmp .halt

runtimeReadTSC:
    rdtsc
    ret

runtimeHalt:
    cli
.halt_loop:
    hlt
    jmp .halt_loop

runtimePause:
    pause
    ret

memset:
    push ebp
    mov ebp, esp
    push edi
    mov edi, [ebp + 8]    ; dest
    movzx eax, byte [ebp + 12] ; value
    mov ecx, [ebp + 16]   ; length
    mov edx, edi          ; preserve dest for return
    cld
    rep stosb
    mov eax, edx
    pop edi
    pop ebp
    ret

memcpy:
    push ebp
    mov ebp, esp
    push edi
    push esi
    mov edi, [ebp + 8]    ; dest
    mov esi, [ebp + 12]   ; src
    mov ecx, [ebp + 16]   ; len
    mov edx, edi          ; preserve dest
    cld
    rep movsb
    mov eax, edx
    pop esi
    pop edi
    pop ebp
    ret

memmove:
    push ebp
    mov ebp, esp
    push edi
    push esi
    mov edi, [ebp + 8]    ; dest
    mov esi, [ebp + 12]   ; src
    mov ecx, [ebp + 16]   ; len
    mov edx, edi          ; preserve dest
    cmp edi, esi
    je .memmove_done
    jb .memmove_forward
    lea esi, [esi + ecx - 1]
    lea edi, [edi + ecx - 1]
    std
    rep movsb
    cld
    jmp .memmove_done
.memmove_forward:
    cld
    rep movsb
.memmove_done:
    mov eax, edx
    pop esi
    pop edi
    pop ebp
    ret
